package com.spark.lola.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.constant.CertificationStatus;
import com.spark.lola.constant.CertificationType;
import com.spark.lola.entity.SupplierToken;
import com.spark.lola.entity.sup.AccountCertificationApply;
import com.spark.lola.entity.sup.AccountInfo;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.mapper.sup.AccountCertificationApplyMapper;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.request.CertificationApplyRequest;
import com.spark.lola.service.sup.IAccountCertificationApplyService;
import com.spark.lola.service.sup.IAccountCertificationService;
import com.spark.lola.service.sup.IAccountInfoService;
import com.spark.lola.support.api.entity.TokenUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.wms.cache.CollectionCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 认证
 *
 * @Author: DengYeJun
 * @Date: 2020/5/22 14:24
 */
@RestController
@RequestMapping(value = "/api/identification", produces = "application/json;charset=utf-8")
@Api(value = "/api/identification", tags = "认证")
public class IdentificationController {
    private static final Logger logger = LoggerFactory.getLogger(IdentificationController.class);
    @Autowired
    private IAccountCertificationApplyService certificationApplyService;

    @Autowired
    private IAccountInfoService accountInfoService;

    @Autowired
    private AccountCertificationApplyMapper accountCertificationApplyMapper;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @ApiOperation(value = "提交认证信息")
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean apply(@RequestBody CertificationApplyRequest request, @SessionAttribute SupplierToken tokenUser) {
        logger.info("===用户【{}】提交认证信息===START===", tokenUser.getUsername());
        // 个人认证-验证姓名
        if (request.getCertifiedType() == CertificationType.PERSONAL) {
            if (supplierInfoMapper.checkName(request.getRealname(), request.getAccountId()) != 0) {
                return ResponseBean.error("真实姓名已存在");
            }
        } else if (accountCertificationApplyMapper.checkCompanyName(request.getCompanyName(), null) != 0) { // 验证企业名称
            return ResponseBean.error("企业名称已存在");
        }
        AccountCertificationApply model = new AccountCertificationApply();
        BeanUtils.copyProperties(request, model);
        model.setAccountId(tokenUser.getMemberId());
        QueryWrapper<AccountCertificationApply> query = new QueryWrapper<>();
        query.eq("account_id", model.getAccountId());
        // 删除当前用户历史认证记录
        certificationApplyService.remove(query);
        // 保存当前用户历史认证记录
        model.setStatus(CertificationStatus.WAIT);
        certificationApplyService.save(model);
        AccountInfo accountInfo = accountInfoService.getById(tokenUser.getMemberId());
        accountInfo.setCertificationStatus(model.getStatus());

        accountInfoService.updateById(accountInfo);
        logger.info("===用户【{}】提交认证信息===END===", tokenUser.getUsername());
        return ResponseBean.success();
    }

    @ApiOperation(value = "获取用户认证信息")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseBean apply(@SessionAttribute SupplierToken tokenUser) {
        SupplierInfo supplierInfo = supplierInfoMapper.selectById(tokenUser.getSupplierId());
        QueryWrapper<AccountCertificationApply> query = new QueryWrapper<>();
        query.eq("account_id", tokenUser.getMemberId());
        List<AccountCertificationApply> lst = certificationApplyService.list(query);
        if (CollectionUtils.isEmpty(lst)) {
            return new ResponseBean(404, "当前用户未提交认证信息");
        }
        AccountCertificationApply model = lst.get(0);
        Long salesmanId = model.getSalesmanId();
        if (supplierInfo != null) {
            salesmanId = supplierInfo.getSalesmanId();
        }
        if (salesmanId != null) {
            model.setSalesman(CollectionCache.employees.get(salesmanId));
        }
        return ResponseBean.success(model);
    }

}
